package com.luke.star.serviceImpl;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.luke.star.enums.AssiCircleModelEnum;
import com.luke.star.enums.AssiStateEnum;
import com.luke.star.enums.AssiTaskModelEnum;
import com.luke.star.helper.serviceImpl.StAssignmentHelperService;
import com.luke.star.persist.mapper.StAssignmentMapper;
import com.luke.star.persist.mapper.StTaskMapper;
import com.luke.star.persist.po.StAssignment;
import com.luke.star.persist.po.StAssignmentLog;
import com.luke.star.persist.po.StTask;
import com.luke.star.persist.po.StTaskNote;
import com.luke.star.util.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class StAssignmentService {

	@Resource
	private StTaskMapper stTaskMapper;
	@Resource
	private StAssignmentMapper stAssignmentMapper;
	@Resource
	private StAssignmentHelperService stAssignmentHelperService;
	
	public void runAssignment() {
		//查询所有符合条件的 (1.分解任务状态正常，2.任务状态正常，3.分解任务当天没有收集过星星,4.在收集周期内)
		List<StAssignment> runlist = stAssignmentMapper.queryRunAssignment();
		int assiCount = 0;
		int starCount = 0;
		for(StAssignment assi:runlist) {
			try {
				StTask taskinfo = stTaskMapper.selectByPrimaryKey(assi.getAssiTaskId());
				if(!isRuningDay(assi)) {
					log.info("今日分解任务[{}],assid[{}]未到执行周期[{}]，不执行",taskinfo.getTaskName()
							,assi.getAssiId(),AssiCircleModelEnum.getDesc(assi.getAssiCircleModel()));
					continue;
				}
				Integer totalstar = taskinfo.getStarNum()==null?0:taskinfo.getStarNum();
				StTask task = new StTask();
				task.setId(assi.getAssiTaskId());
				task.setUpdateTime(DateUtil.getNowDate());
				task.setStarNum(totalstar+assi.getAssiExpectNum());
				
				StTaskNote tasknote = new StTaskNote();
				tasknote.setTaskId(taskinfo.getId());
				tasknote.setCreateTime(DateUtil.getNowDate());
				tasknote.setAddStarNum(assi.getAssiExpectNum());
				tasknote.setNowStarNum(task.getStarNum());
				tasknote.setNote("“"+taskinfo.getTaskName()+"”任务分解定时任务加星【"+assi.getAssiExpectNum()+"】");
				
				StAssignment assiRecord = new StAssignment();
				assiRecord.setAssiId(assi.getAssiId());
				assiRecord.setAssiRunTime(assi.getAssiRunTime()+1);
				assiRecord.setAssiUpdateTime(DateUtil.getNowDate());
				if(AssiTaskModelEnum.fullStop.code.equals(assi.getAssiTaskModel()) && task.getStarNum()>=taskinfo.getExpectNum()) {
					assiRecord.setAssiState(AssiStateEnum.finish.code);
				}
				
				StAssignmentLog assiLog = new StAssignmentLog();
				assiLog.setAssiId(assi.getAssiId());
				assiLog.setCreateTime(DateUtil.getNowDate());
				assiLog.setNote("“"+taskinfo.getTaskName()+"”任务分解加星【"+assi.getAssiExpectNum()+"】");
				
				stAssignmentHelperService.updateAssiJobData(task, tasknote, assiRecord, assiLog);
				assiCount++;
				log.info("分解任务[{}]收集[{}]颗星星成功，assiId[{}]",taskinfo.getTaskName(),assi.getAssiExpectNum(),assi.getAssiId());
			} catch (Exception e) {
				log.error("收集任务分解报错assiId[{}],taskId[{}],",assi.getAssiId(),assi.getAssiTaskId(),e);
			}
			
		}
		log.info("分解任务星星收集汇总，分解任务数量[{}],收集成功数量[{}],收集星星总数[{}]",runlist.size(),assiCount,starCount);
		
	}
	
	private boolean isRuningDay(StAssignment assi) {
		String circleModel = assi.getAssiCircleModel();
		
		if(AssiCircleModelEnum.day.code.equals(circleModel)){
			return true;
		}else if(AssiCircleModelEnum.workday.code.equals(circleModel)){
			Calendar c = Calendar.getInstance();
			if(Calendar.SUNDAY != c.get(Calendar.DAY_OF_WEEK) && Calendar.SATURDAY != c.get(Calendar.DAY_OF_WEEK)) {
				return true;
			}
		}else if(AssiCircleModelEnum.weekend.code.equals(circleModel)){
			Calendar c = Calendar.getInstance();
			if(Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK) && Calendar.SATURDAY == c.get(Calendar.DAY_OF_WEEK)) {
				return true;
			}
		}else if(AssiCircleModelEnum.week.code.equals(circleModel)){
			Calendar c = Calendar.getInstance();
			if(Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK)) {
				return true;
			}
		}else if(AssiCircleModelEnum.month.code.equals(circleModel)){
			Calendar c = Calendar.getInstance(); 
			c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH)+1); 
			if(c.get(Calendar.DAY_OF_MONTH) == 1) {
				return true;
			}
		}else if(AssiCircleModelEnum.year.code.equals(circleModel)){
			Calendar c = Calendar.getInstance(); 
			c.set(Calendar.DAY_OF_YEAR,c.get(Calendar.DAY_OF_YEAR)+1); 
			if(c.get(Calendar.DAY_OF_YEAR) == 1) {
				return true;
			}
		}else {
			log.error("错误的周期配置：assiId[{}],circleModel[{}]",assi.getAssiId(),assi.getAssiCircleModel());
		}
		return false;
	}
	
	
}
